const jwt = require('jsonwebtoken')
module.exports = (req, res, next) => {
  const url = req.originalUrl

  const whiteUrl = ['/user/login', '/user/register', '/user/upload', '/user/forget']
  if (whiteUrl.find((item) => item === url)) {
    next()
    return false
  }

  const token = req.headers['c-token'] || ''
  if (!token) {
    return res.json({ errcode: 50000, msg: 'token缺失，请重新登陆' })
  }

  jwt.verify(token, 'azrael', (err, decode) => {
    if (!err) {
      next()
    } else {
      return res.json({ errcode: 50000, msg: 'token失效，请重新登陆' })
    }
  })
}
